﻿using System;
using System.Collections.Generic;
using System.Text;
using ET;
using Marx;
using Marx.Utils;

namespace Server
{

    public class ServiceSender
    {
        public long ActorId;

        public ETTask<IActorResponse> Call(IActorRequest req)
        {
            return MessageHelper.CallActor(ActorId, req);
        }

        public void Send(IActorMessage req)
        {
            MessageHelper.SendActor(ActorId, req);
        }
    }

    public class ServiceManager : Singleton<ServiceManager>
    {
        private Dictionary<int, ServiceSender> actors_ = new Dictionary<int, ServiceSender>();

        public async ETTask<ServiceSender> GetServiceSender(int zone, string service)
        {
            var id = GetServiceId(zone, service);
            if(actors_.TryGetValue(id, out var sender))
            {
                return sender;
            }

            sender = new ServiceSender();
            sender.ActorId = await LocationProxyComponent.Instance.Get(id);
            actors_.Add(id, sender);
            return sender;
        }

        public int GetServiceId(int zone, string service)
        {
            var config = ServerConfigration.Instance.GetServiceConfig(service);
            return config.GetID(zone);
        }
    }
}
